from tortoise import fields
from tortoise.models import Model
import datetime


class TimestampMixin:
    id = fields.IntField(pk=True, generated=True, description='API Paramater ID')
    create_ts = fields.DatetimeField(null=True, auto_now_add=True, description="create time")
    update_ts = fields.DatetimeField(null=True, auto_now=True, description="update time")


class OtherMixin:
    create_user = fields.CharField(max_length=32, description="create user")
    update_user = fields.CharField(max_length=32, description="update user")
    is_delete = fields.IntField(description="delete status 0 default 1 del", default=0)


class UidMixin:
    uid = fields.CharField(max_length=32, index=True, description="uid")


class TenantMixin:
    tenant_id = fields.CharField(max_length=32, index=True, description="tenant id")
    e_id = fields.CharField(max_length=32, index=True, description="e id")


class BaseModel(Model):

    async def list_to_dict(self):
        request_dict = {f: getattr(self, f) for f in self._meta.fields}
        request_dict['create_ts'] = datetime.datetime.strftime(request_dict['create_ts'], '%Y-%m-%d')
        request_dict['update_ts'] = datetime.datetime.strftime(request_dict['update_ts'], '%Y-%m-%d')
        return request_dict
